package com.cetsk.android.talkorigins;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 6;
	
	static final String DBNAME = "CCIndex";
	static final String FAVORITES_TABLE = "Favorites";
	static final String CLAIMS_TABLE = "Claims";
	
	public DatabaseHelper(Context context) {
		super(context, DBNAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS " + FAVORITES_TABLE + " (Id INTEGER PRIMARY KEY AUTOINCREMENT, ClaimId INTEGER)");
		db.execSQL("CREATE TABLE IF NOT EXISTS " + CLAIMS_TABLE + " (Id INTEGER PRIMARY KEY AUTOINCREMENT, ParentId INTEGER, Key TEXT, Name TEXT, Url TEXT, Content TEXT)");
		db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_url ON " + FAVORITES_TABLE + " (ClaimId)");
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if(newVersion == 6){
			db.execSQL("DROP TABLE IF EXISTS " + FAVORITES_TABLE );
			db.execSQL("DROP TABLE IF EXISTS " + CLAIMS_TABLE );
		}
		onCreate(db);
		
	}
}